package com.amikomgamedev.panjatpinangGLES1.states;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.timer.ITimerCallback;
import org.anddev.andengine.engine.handler.timer.TimerHandler;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.modifier.FadeOutModifier;
import org.anddev.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.modifier.IModifier;
import org.anddev.andengine.util.modifier.ease.EaseLinear;

import android.content.Intent;

import com.amikomgamedev.panjatpinangGLES1.Config;
import com.amikomgamedev.panjatpinangGLES1.Data;
import com.amikomgamedev.panjatpinangGLES1.Game;

public class State_Logo extends BaseGameActivity {

	protected Camera mCamera;
	protected Scene mScene;

	public static Texture logoTex;
	public static TextureRegion logoTexReg;
	private Sprite logoSpr;

	@Override
	public void onLoadComplete() {
		// TODO Auto-generated method stub

	}

	@Override
	public Engine onLoadEngine() {
		// TODO Auto-generated method stub
		Game.appInit();
		mCamera = new Camera(0, 0, Config.GAME_SCREEN_WIDTH,
				Config.GAME_SCREEN_HEIGHT);
		Engine en = new Engine(new EngineOptions(true,
				ScreenOrientation.PORTRAIT, new RatioResolutionPolicy(
						Config.GAME_SCREEN_WIDTH, Config.GAME_SCREEN_HEIGHT),
				mCamera));
		return en;
	}

	@Override
	public void onLoadResources() {
		// TODO Auto-generated method stub
		logoTex = new Texture(512, 512,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		logoTexReg = TextureRegionFactory.createFromAsset(logoTex, this,
				Data.IMG_AGD_LOGO_LOCATION, 0, 0);

		mEngine.getTextureManager().loadTexture(logoTex);

	}

	@Override
	public Scene onLoadScene() {
		// TODO Auto-generated method stub
		this.mEngine.registerUpdateHandler(new FPSLogger());

		mScene = new Scene(1);
		mScene.setBackground(new ColorBackground(0xff, 0xff, 0xff));

		int centerX = (Config.GAME_SCREEN_WIDTH >> 1)
				- (logoTexReg.getWidth() >> 1);
		int centerY = (Config.GAME_SCREEN_HEIGHT >> 1)
				- (logoTexReg.getHeight() >> 1);

		logoSpr = new Sprite(centerX, centerY, logoTexReg);

		mScene.getLastChild().attachChild(logoSpr);

		this.mEngine.registerUpdateHandler(new TimerHandler(
				Config.LOGO_DURATION, new ITimerCallback() {

					// @Override
					// public void onTimePassed(TimerHandler arg0) {
					// // TODO Auto-generated method stub
					// Intent i = new Intent(State_Logo.this,
					// State_Menu_Main.class);
					// startActivity(i);
					// finish();
					// }

					public void onTimePassed(TimerHandler arg0) {
						// TODO Auto-generated method stub
						FadeOutModifier prFadeOutModifier = new FadeOutModifier(
								0.5f, new IEntityModifierListener() {

									// @Override
									public void onModifierFinished(
											IModifier<IEntity> arg0,
											IEntity arg1) {
										Intent intent = new Intent(
												State_Logo.this,
												State_Menu_Main.class);
										startActivity(intent);
										finish();

									}
								}, EaseLinear.getInstance());
						logoSpr.setBlendFunction(GL10.GL_SRC_ALPHA,
								GL10.GL_ONE_MINUS_SRC_ALPHA);
						logoSpr.registerEntityModifier(prFadeOutModifier);
					}

				}));
		return mScene;
	}
}
